package com.business.system.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.rose.jdbc.KeyGenerator;
import net.sf.rose.jdbc.PageBean;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.web.utils.WebUtils;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.business.system.bean.GoodsStoreBean;
import com.business.system.bean.GoodsStoreDetailBean;
import com.business.system.util.DBTools;
import com.business.system.util.PageUtil;

/**
 * @Description:库存处理
 * @author: zhangqiang
 * @Date: 8:49 2018/4/17
 */
@RestController
@RequestMapping("store")
public class StoreAction {

    /**
     * @Description:获取库存，分页
     * @Author: zhangqiang
     * @Date: 16:16 2018/4/14
     */
    @RequestMapping("getStoreListByPage.do")
    public PageBean getStoreListByPage(Service service, HttpServletRequest request) {
        PageBean page = PageUtil.page(request);
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT t3.BRAND_NAME,t1.STOCK_COUNT,t2.* FROM goods_store t1 LEFT JOIN goods t2 on t1.GOODS_ID = t2.ID LEFT JOIN brand t3 on t2.BRAND_ID = t3.ID where 1=1 ");
        Map<String, Object> params = WebUtils.getRequestData(request);
        if (params.containsKey("content")) {
            sql.append(" and (t2.GOODS_NAME like '%" + params.get("content") + "%' or t2.GOODS_NO like '%"+params.get("content")+"%' )");
        }

        if (params.containsKey("brand")) {
            sql.append(" and t2.BRAND_ID = '"+params.get("brand")+"'");
        }

        DBTools.getDataList(service, sql.toString(), page);
        return page;
    }



    /**
     * @Description:批量入库
     * @Author: zhangqiang
     * @Date: 15:24 2018/4/18
     */
    @RequestMapping("startStoreIn.do")
    public int startStoreIn(Service service, HttpServletRequest request) {
        Map<String,Object> map = WebUtils.getRequestData(request);
        List<GoodsStoreDetailBean>list = JSON.parseArray(map.get("data").toString(),GoodsStoreDetailBean.class);
        int flag = 0;
        for (GoodsStoreDetailBean goodsStoreDetailBean : list) {
            GoodsStoreBean goodsStoreBean = DBTools.getBean(service, GoodsStoreBean.class, "SELECT * FROM `goods_store` where GOODS_ID = ?", goodsStoreDetailBean.getGoodsId());
            goodsStoreDetailBean.setId(KeyGenerator.getSystemUUID());
            //保存变动后的库存
            goodsStoreDetailBean.setStockCount(goodsStoreBean.getStockCount() + goodsStoreDetailBean.getChangeCount());
            if (DBTools.insertBean(service, goodsStoreDetailBean) == 1) {
                goodsStoreBean.setStockCount(goodsStoreBean.getStockCount() + goodsStoreDetailBean.getChangeCount());
                flag = DBTools.updateBean(service, goodsStoreBean);
            }
        }
        return flag;
    }


    /**
     * @Description:批量出库
     * @Author: zhangqiang
     * @Date: 15:24 2018/4/18
     */
    @RequestMapping("startStoreOut.do")
    public int startStoreOut(Service service, HttpServletRequest request) {
        Map<String, Object> map = WebUtils.getRequestData(request);
        List<GoodsStoreDetailBean> list = JSON.parseArray(map.get("data").toString(), GoodsStoreDetailBean.class);
        int flag = 0;
        for (GoodsStoreDetailBean goodsStoreDetailBean : list) {
            GoodsStoreBean goodsStoreBean = DBTools.getBean(service, GoodsStoreBean.class, "SELECT * FROM `goods_store` where GOODS_ID = ?", goodsStoreDetailBean.getGoodsId());
            goodsStoreDetailBean.setId(KeyGenerator.getSystemUUID());
            //保存变动后的库存
            goodsStoreDetailBean.setStockCount(goodsStoreBean.getStockCount() - goodsStoreDetailBean.getChangeCount());
            if (DBTools.insertBean(service, goodsStoreDetailBean) == 1) {
                goodsStoreBean.setStockCount(goodsStoreBean.getStockCount() - goodsStoreDetailBean.getChangeCount());
                flag = DBTools.updateBean(service, goodsStoreBean);
            }
        }
        return flag;
    }


    /**
     * @Description:查询库存详细记录
     * @Author: zhangqiang
     * @Date: 13:30 2018/4/19
     */
    @RequestMapping("getGoodsStoreDetail.do")
    public PageBean getGoodsStoreDetail(Service service, HttpServletRequest request) {
        Map<String, Object> params = WebUtils.getRequestData(request);
        PageBean page = PageUtil.page(request);
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT case  " +
                "when t1.TYPE=1 AND t1.DETAIL_TYPE=1 then 1 " +
                "when t1.TYPE=1 AND t1.DETAIL_TYPE=2 then 2 " +
                "when t1.TYPE=2 AND t1.DETAIL_TYPE=1 then 3 " +
                "when t1.TYPE=2 AND t1.DETAIL_TYPE=2 then 4 " +
                "when t1.TYPE=2 AND t1.DETAIL_TYPE=3 then 5 " +
                "else 6 END as 'FINAL_TYPE',t1.*,t2.GOODS_NAME,t2.GOODS_NO FROM goods_store_detail t1 LEFT JOIN goods t2 on t1.GOODS_ID = t2.ID where 1=1 ");

        sql.append(" AND t1.GOODS_ID= '" + params.get("goodsId") + "'");

        if (params.containsKey("content")) {
            sql.append(" and (t2.GOODS_NAME like '%" + params.get("content") + "%' or t2.GOODS_NO like '%" + params.get("content") + "%' )");
        }

        if (params.containsKey("type")) {
            String type = params.get("type").toString();
            String temp_str = "";

            switch (type) {
                //入库-日常业务
                case "1":
                    temp_str = " and t1.TYPE='1' AND t1.DETAIL_TYPE='1' ";
                    break;
                //入库-盘盈
                case "2":
                    temp_str = " and t1.TYPE='1' AND t1.DETAIL_TYPE='2' ";
                    break;
                //出库-日常
                case "3":
                    temp_str = " and t1.TYPE='2' AND t1.DETAIL_TYPE='1' ";
                    break;
                //出库-盘盈
                case "4":
                    temp_str = " and t1.TYPE='2' AND t1.DETAIL_TYPE='2' ";
                    break;
                //销售
                case "5":
                    temp_str = " and t1.TYPE='2' AND t1.DETAIL_TYPE='3' ";
                    break;
                default:
                    temp_str = " ";
                    break;

            }

            sql.append(temp_str);
        }

        if (params.containsKey("beginTime")) {

            sql.append(" and (t1.CREATE_TIME >= '" + params.get("beginTime").toString() + " 00:00:00' )");
        }
        if (params.containsKey("endTime")) {
            sql.append(" and (t1.CREATE_TIME <= '" + params.get("endTime") + " 23:59:59') ");
        }

        sql.append(" order by t1.CREATE_TIME desc");
        DBTools.getDataList(service, sql.toString(), page);
        return page;
    }


}